﻿package net.wangit.dev.unit;

import net.wangit.Unit;
import net.wangit.context.ActionContext;
import net.wangit.dev.ConsoleDomainUtil;
import net.wangit.dev.ReferenceUtil;
import net.wangit.jdbc.util.DBHelper;
import net.wangit.support.UnitInterceptor;
import net.wangit.util.Checker;

public class UnitReferenceUI implements UnitInterceptor {

	public UnitReferenceUI() {
	}

	public String intercept(ActionContext ac, Unit unit) throws Exception {
		String c = ac.getRequestParameter("c");
		String id = ac.getRequestParameter("id");
		String domain = ConsoleDomainUtil.getDomain(ac);
		if (!Checker.isEmpty(domain) && !Checker.isEmpty(c)
				&& !Checker.isEmpty(id) && Checker.isInteger(c)) {
			net.wangit.jdbc.DB db = DBHelper.getDomainDB(domain);
			net.wangit.MapList data;
			if ("101".equals(c)) {
				data = ReferenceUtil.getTableReference(db, id);
				unit.removeElement("eid");
				unit.removeElement("n");
			} else if ("102".equals(c))
				data = ReferenceUtil.getEnumerationReference(db, id);
			else if ("110".equals(c))
				data = ReferenceUtil.getDataPrivilegeReference(db, id);
			else
				data = ReferenceUtil.getReference(db, c, id);
			unit.setData(data);
		}
		return unit.write(ac);
	}
}
